var app = getApp();
var drawProgress = require("../../../js/drawcircle.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    publishid: '',
    list: [],
    user_type: 1,
    oclockDetail: null,
    detail: {},
    usid: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var user_type = wx.getStorageSync('user_type');
    var usid = wx.getStorageSync('usid');
    this.setData({
      publishid: options.publishid,
      user_type: user_type,
      usid: usid
    })
    this.getWorkDetail()
    if(user_type=='2'){
      this.workPageFinish()
    }else{
      this.getOclockCount()
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getWorkDetail () {
    const that = this
    const publishid = this.data.publishid
    wx.showLoading({
      title: '加载中',
    })
    app.wxget({
      url: 'issuedWorks/workDetail.html?publishid='+publishid,
      success: function (res) {
        wx.hideLoading()
        var detail = res.data
        that.setData({
          detail: detail
        })
      }
    })
  },

  workPageFinish () {
    const that = this
    const publishid = this.data.publishid
    app.wxget({
      url: 'issuedWorks/workPageFinish.html?publishid='+publishid+"&page=1&limit=10",
      success: function (res) {
        wx.hideLoading()
        var list = res.data.list
        that.setData({
          list: list
        })
      }
    })
  },

  //查看打卡列表
  toOclockList(e) {
    const publishid = this.data.publishid
    wx.navigateTo({
      url: '../searchOclock/index?publishid=' + publishid
    })
  },

  //查看打开详情
  toOclockDetail(e) {
    const publishid = e.currentTarget.dataset.publishid
    const usid = e.currentTarget.dataset.usid
    const percent = e.currentTarget.dataset.percent
    wx.navigateTo({
      url: '../oclockDetail/index?publishid=' + publishid + '&usid=' + usid + '&percent=' + percent
    })
  },

  getOclockCount() {
    const usid = wx.getStorageSync('usid');
    const that = this
    const publishid = this.data.publishid
    app.wxget({
      url: 'issuedWorks/userOclockStatistics.html?usid='+usid+"&publishid="+publishid,
      success: function (res) {
        var oclockDetail = res.data
        that.setData({
          oclockDetail: oclockDetail
        })

        //x坐标，y坐标，r半径，计数器最大值60,对应 2
        drawProgress.drawProgress(75, 75, 65, 0.6 * parseInt(oclockDetail.PERCENT));
      }
    })
  },

  //我要打卡
  oclocktoDay(e) {
    const publishid = this.data.publishid
    const daylimit = this.data.detail.DAYLIMIT
    const oclocktype = e.currentTarget.dataset.oclocktype
    const aleady = this.data.oclockDetail.aleady
    if (oclocktype == '0') {
      if (aleady == '1') {
        wx.showToast({
          title: '你今天已经打卡了',
          icon: 'error',
          duration: 2000
        })
        return
      }
    }
    wx.redirectTo({
      url: '../oclockAdd/index?publishid=' + publishid + '&daylimit=' + daylimit + '&oclocktype=' + oclocktype
    })
  }

})